package com.best.oasis.settlement.dao.security;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.best.oasis.settlement.common.entity.security.Role;
import com.best.oasis.settlement.common.entity.security.RoleGroupMapping;
import com.best.oasis.settlement.common.so.security.RoleSO;
import com.best.oasis.settlement.dao.base.BaseDaoImpl;
import com.best.oasis.settlement.util.exception.DAOException;

@Repository
public class RoleDaoImpl extends BaseDaoImpl<Role, Long> implements RoleDao {
    /**
     * Logger for this class
     */
    private static final Logger   logger                           = Logger.getLogger(RoleDaoImpl.class);

    protected static final String GET_BY_ROLE_NAME                 = "getByRoleName";

    protected static final String DELETE_USERROLE_BY_ROLE_ID       = "deleteUserRoleByRoleId";
    protected static final String DELETE_ROLEGROUP_ROLE_BY_ROLE_ID = "deleteRoleGroupRoleByRoleId";
    protected static final String GET_LIST_BY_PARENT_ID            = "getByParentId";
    protected static final String GET_ROOT_ROLE                    = "getRootRole";
    protected static final String CHECK_BY_ROLE                    = "getRoleGroupRoleCountByRoleId";

    /**
     * Constructor to create a Generics-based version using Role as the entity
     */
    public RoleDaoImpl() {
        super(Role.class);
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public Role getRoleByName(String roleName) {

        List<Role> roles = (List<Role>) sqlSessionTemplate.selectList(getStatementPrefix() + GET_BY_ROLE_NAME,
                roleName);

        if (roles.isEmpty()) {
            return null;
        } else {
            return (Role) roles.get(0);
        }
    }

    @SuppressWarnings("unchecked")
    public List<Role> getAll(RoleSO so) {
        List<Role> retList = (List<Role>) sqlSessionTemplate.selectList(
                getStatementPrefix() + POSTFIX_GET_LIST_BY_SO, so);
        return retList;
    }

    /**
     * {@inheritDoc}
     */
    public void removeRole(String rolename) {
        Role role = getRoleByName(rolename);

        Long roleId = role.getId();
        //删除Role前需要先删除引用到该role的User和Rolegroup与该Role的关系
        sqlSessionTemplate.delete(getStatementPrefix() + DELETE_USERROLE_BY_ROLE_ID, roleId);
        sqlSessionTemplate.delete(getStatementPrefix() + DELETE_ROLEGROUP_ROLE_BY_ROLE_ID, roleId);

        sqlSessionTemplate.delete(getStatementPrefix() + POSTFIX_DELETE_BY_ID, roleId);
    }

    /**
     * 根据parentId获取所有节点
     * 
     * @param parentId
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Role> getAllChildren(Long parentId) {
        List<Role> result = (List<Role>) sqlSessionTemplate.selectList(
                getStatementPrefix() + GET_LIST_BY_PARENT_ID, parentId);
        return result;
    }

    /**
     * 获取ROOT节点
     * 
     * @return
     */
    @SuppressWarnings("unchecked")
    public Role getRootRole() {
        List<Role> list = (List<Role>) sqlSessionTemplate.selectList(getStatementPrefix() + GET_ROOT_ROLE);

        Role result = null;
        if (list != null && list.size() > 0) {
            result = list.get(0);
        }
        return result;
    }

    @Override
    public boolean checkByRole(Long roleId) {
    	Long count = (Long) sqlSessionTemplate.selectOne(
                getStatementPrefix() + CHECK_BY_ROLE, roleId);
        if (count.intValue() > 0) {
            return true;
        } else {
            return false;
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Role> getRolesByUserID(Long userID) {
        List<Role> list = (List<Role>) sqlSessionTemplate.selectList(getStatementPrefix() + "getRolesByUserID",
                userID);
        return list;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Role> getRolesByRoleGroupId(Long id) {
        List<Role> list = (List<Role>) sqlSessionTemplate.selectList(
                getStatementPrefix() + "getRolesByRoleGroupId", id);
        return list;
    }
    
    
    @Override
    public void removeRoleGroupRole(Long groupid) {
        try {
            sqlSessionTemplate.delete(getStatementPrefix() + "deleteRoleGroupRole", groupid);
        } catch (Exception e) {
            logger.error(getStatementPrefix() + "error occured when method deleteRoleGroupRole invocked.");
            logger.error(e);
            e.printStackTrace();
            throw new DAOException(e);
        }
    }

    @Override
    public void createRoleGroupRole(RoleGroupMapping object) {
        try {
            sqlSessionTemplate.insert(getStatementPrefix() + "insertRoleGroupRole", object);
        } catch (Exception e) {
            logger.error(getStatementPrefix() + "error occured when method insertRoleGroupRole invocked.");
            logger.error(e);
            e.printStackTrace();
            throw new DAOException(e);
        }
    }
}
